{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "matplotlib.use('TkAgg')\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "matplotlib.rcParams['font.sans-serif'] = ['SimHei']\n",
    "import datawash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 遍历所有用户，读取需要的信息\n",
    "voteupCountList = list() # 赞同数\n",
    "thankedCountList = list() # 感谢数\n",
    "followingCountList = list() # 关注数\n",
    "followerCountList = list() # 关注者数\n",
    "favoriteCountList = list() # 收藏数\n",
    "favoritedCountList = list() # 被收藏数\n",
    "answerCountList = list() # 回答数\n",
    "articlesCountList = list() # 文章数\n",
    "questionCountList = list() # 提问数\n",
    "followingQuestionCountList = list() # 关注问题数\n",
    "followingTopicCountList = list() # 关注话题数\n",
    "followingFavlistsCountList = list() #  关注收藏夹数\n",
    "followingColumnsCountList = list() # 关注专栏数\n",
    "\n",
    "jsons = datawash.datajsons()\n",
    "for user in jsons:\n",
    "    try:\n",
    "        voteupCountList.append(user['voteupCount'])\n",
    "        thankedCountList.append(user['thankedCount'])\n",
    "        followingCountList.append(user['followingCount'])\n",
    "        followerCountList.append(user['followerCount'])\n",
    "        favoriteCountList.append(user['favoriteCount'])\n",
    "        favoritedCountList.append(user['favoritedCount'])\n",
    "        answerCountList.append(user['answerCount'])\n",
    "        articlesCountList.append(user['articlesCount'])\n",
    "        questionCountList.append(user['questionCount'])\n",
    "        followingQuestionCountList.append(user['followingQuestionCount'])\n",
    "        followingTopicCountList.append(user['followingTopicCount'])\n",
    "        followingFavlistsCountList.append(user['followingFavlistsCount'])\n",
    "        followingColumnsCountList.append(user['followingColumnsCount'])\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 绘图用到的颜色\n",
    "black = '#212121'\n",
    "gray = '#727272'\n",
    "red = '#D32F2F'\n",
    "orange = '#FF9500'\n",
    "orange2 = '#FFF1DE'\n",
    "green = '#99cc33'\n",
    "brown = '#cc6600'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'voteupCountList' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-5b3254cce85e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     25\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msubplotlist\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m     \u001b[0mtempList\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mx\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvoteupCountList\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m>=\u001b[0m\u001b[0medge\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m<\u001b[0m\u001b[0medge\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m     \u001b[0mvoteupCountArray\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtempList\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m     \u001b[0mtempList\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mx\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mthankedCountList\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m>=\u001b[0m\u001b[0medge\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m<\u001b[0m\u001b[0medge\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'voteupCountList' is not defined"
     ]
    }
   ],
   "source": [
    "# 72万知乎用户获得赞同数和感谢数分布\n",
    "# voteupCount 赞同数\n",
    "# thankedCount 感谢数\n",
    "fig, axes = plt.subplots(3,2)\n",
    "fig.set_size_inches(18,10)\n",
    "fig.suptitle('72万知乎用户获得赞同数和感谢数分布', fontsize=16, color=red)\n",
    "fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.5)\n",
    "\n",
    "# 把一个二维子图数组压扁\n",
    "subplotlist = list()\n",
    "for i in axes:\n",
    "    for j in i:\n",
    "        subplotlist.append(j)\n",
    "\n",
    "# 每个直方图的统计范围\n",
    "edge = [[0,100],\n",
    "       [100,1000],\n",
    "       [1000,10000],\n",
    "       [10000,100000],\n",
    "       [100000,1000000],\n",
    "       [1000000,4000000]\n",
    "       ]\n",
    "# 每个直方图的组距\n",
    "widthlist = [1, 5, 50, 500, 5000, 50000]\n",
    "\n",
    "for i in range(len(subplotlist)):\n",
    "    tempList = [x for x in voteupCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    voteupCountArray = np.array(tempList)\n",
    "    tempList = [x for x in thankedCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    thankedCountArray = np.array(tempList)\n",
    "    subplotlist[i].hist([voteupCountArray,thankedCountArray], normed=0, histtype='barstacked', label=['赞同数','感谢数'],\n",
    "                        bins=int((edge[i][1]-edge[i][0])/widthlist[i]), color=[red,orange], alpha = 0.7)\n",
    "    subplotlist[i].legend(loc='best')\n",
    "    subplotlist[i].set_xlim(edge[i][0], edge[i][1])\n",
    "    subplotlist[i].set_title('%d-%d赞同数和感谢数分布（组距：%d）'%(edge[i][0],edge[i][1],widthlist[i]), color=red)\n",
    "    subplotlist[i].set_xlabel('赞同数和感谢数',color=red)\n",
    "    subplotlist[i].set_ylabel('用户数量（人）',color=red)\n",
    "    subplotlist[i].set_facecolor(orange2)\n",
    "    subplotlist[i].grid(True, linestyle='--')\n",
    "\n",
    "# 微调\n",
    "axes[0,0].set_ylim(0,300000)\n",
    "subplotlist[5].set_ylim(0,10)\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 72万知乎用户关注数和关注者数分布\n",
    "# followingCount # 关注数\n",
    "# followerCount # 关注者数\n",
    "fig, axes = plt.subplots(3,2)\n",
    "fig.set_size_inches(18,10)\n",
    "fig.suptitle('72万知乎用户关注数和关注者数分布', fontsize=16, color=red)\n",
    "fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.5)\n",
    "\n",
    "# 把一个二维数组压扁\n",
    "subplotlist = list()\n",
    "for i in axes:\n",
    "    for j in i:\n",
    "        subplotlist.append(j)\n",
    "\n",
    "# 每个直方图的统计范围\n",
    "edge = [[0,100],\n",
    "       [100,1000],\n",
    "       [1000,10000],\n",
    "       [10000,100000],\n",
    "       [100000,1000000],\n",
    "       [1000000,3000000]\n",
    "       ]\n",
    "# 每个直方图的组距\n",
    "widthlist = [1, 5, 50, 500, 5000, 50000]\n",
    "\n",
    "for i in range(len(subplotlist)):\n",
    "    tempList = [x for x in followingCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followingCountArray = np.array(tempList)\n",
    "    tempList = [x for x in followerCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followerCountArray = np.array(tempList)\n",
    "    subplotlist[i].hist([followingCountArray,followerCountArray], normed=0, histtype='barstacked', label=['关注数','关注者数'],\n",
    "                        bins=int((edge[i][1]-edge[i][0])/widthlist[i]), color=[red,orange], alpha = 0.7)\n",
    "    subplotlist[i].legend(loc='best')\n",
    "    subplotlist[i].set_xlim(edge[i][0], edge[i][1])\n",
    "    subplotlist[i].set_title('%d-%d关注数和关注者数分布（组距：%d）'%(edge[i][0],edge[i][1],widthlist[i]), color=red)\n",
    "    subplotlist[i].set_xlabel('关注数和关注者数',color=red)\n",
    "    subplotlist[i].set_ylabel('用户数量（人）',color=red)\n",
    "    subplotlist[i].set_facecolor(orange2)\n",
    "    subplotlist[i].grid(True, linestyle='--')\n",
    "\n",
    "# 微调\n",
    "subplotlist[5].set_ylim(0,10)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 72万知乎用户收藏答案数和答案被收藏数分布\n",
    "# favoriteCount 收藏答案数\n",
    "# favoritedCount 答案被收藏数\n",
    "fig, axes = plt.subplots(3,2)\n",
    "fig.set_size_inches(18,10)\n",
    "fig.suptitle('72万知乎用户答案收藏数和答案被收藏数分布', fontsize=16, color=red)\n",
    "fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.5)\n",
    "\n",
    "# 把一个二维数组压扁\n",
    "subplotlist = list()\n",
    "for i in axes:\n",
    "    for j in i:\n",
    "        subplotlist.append(j)\n",
    "\n",
    "# 每个直方图的统计范围\n",
    "edge = [[0,100],\n",
    "       [100,1000],\n",
    "       [1000,10000],\n",
    "       [10000,100000],\n",
    "       [100000,1000000],\n",
    "       [1000000,3000000]\n",
    "       ]\n",
    "# 每个直方图的组距\n",
    "widthlist = [1, 5, 50, 500, 5000, 50000]\n",
    "\n",
    "for i in range(len(subplotlist)):\n",
    "    tempList = [x for x in favoriteCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    favoriteCountArray = np.array(tempList)\n",
    "    tempList = [x for x in favoritedCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    favoritedCountArray = np.array(tempList)\n",
    "    subplotlist[i].hist([favoriteCountArray,favoritedCountArray], normed=0, histtype='barstacked', label=['收藏答案数','答案被收藏数'],\n",
    "                        bins=int((edge[i][1]-edge[i][0])/widthlist[i]), color=[red,orange], alpha = 0.7)\n",
    "    subplotlist[i].legend(loc='best')\n",
    "    subplotlist[i].set_xlim(edge[i][0], edge[i][1])\n",
    "    subplotlist[i].set_title('%d-%d收藏答案数和答案被收藏数分布（组距：%d）'%(edge[i][0],edge[i][1],widthlist[i]), color=red)\n",
    "    subplotlist[i].set_xlabel('收藏答案数和答案被收藏数',color=red)\n",
    "    subplotlist[i].set_ylabel('用户数量（人）',color=red)\n",
    "    subplotlist[i].set_facecolor(orange2)\n",
    "    subplotlist[i].grid(True, linestyle='--')\n",
    "    \n",
    "# 微调\n",
    "subplotlist[5].set_ylim(0,10)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 72万知乎用户回答数，文章数和提问数分布\n",
    "# answerCount 回答数\n",
    "# articlesCount 文章数\n",
    "# questionCount 提问数\n",
    "fig, axes = plt.subplots(2,2)\n",
    "fig.set_size_inches(18,10)\n",
    "fig.suptitle('72万知乎用户回答数，文章数和提问数分布', fontsize=16, color=red)\n",
    "fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.5)\n",
    "\n",
    "# 把一个二维数组压扁\n",
    "subplotlist = list()\n",
    "for i in axes:\n",
    "    for j in i:\n",
    "        subplotlist.append(j)\n",
    "\n",
    "# 每个直方图的统计范围\n",
    "edge = [[0,100],\n",
    "       [100,1000],\n",
    "       [1000,10000],\n",
    "       [10000,40000],\n",
    "       ]\n",
    "# 每个直方图的组距\n",
    "widthlist = [1, 5, 50, 500]\n",
    "\n",
    "for i in range(len(subplotlist)):\n",
    "    tempList = [x for x in answerCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    answerCountArray = np.array(tempList)\n",
    "    tempList = [x for x in articlesCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    articlesCountArray = np.array(tempList)\n",
    "    tempList = [x for x in questionCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    questionCountArray = np.array(tempList)\n",
    "    \n",
    "    subplotlist[i].hist([answerCountArray,articlesCountArray,questionCountArray], normed=0, histtype='barstacked', \n",
    "                        label=['回答数', '文章数', '提问数'],  \n",
    "                        bins=int((edge[i][1]-edge[i][0])/widthlist[i]), color=[red,orange,green], alpha = 0.7)\n",
    "    subplotlist[i].legend(loc='best')\n",
    "    subplotlist[i].set_xlim(edge[i][0], edge[i][1]) \n",
    "    subplotlist[i].set_title('%d-%d回答数，文章数和提问数分布（组距：%d）'%(edge[i][0],edge[i][1],widthlist[i]), color=red)\n",
    "    subplotlist[i].set_xlabel('回答数，文章数和提问数',color=red)\n",
    "    subplotlist[i].set_ylabel('用户数量（人）',color=red)\n",
    "    subplotlist[i].set_facecolor(orange2)\n",
    "    subplotlist[i].grid(True, linestyle='--')\n",
    "\n",
    "# 微调\n",
    "subplotlist[3].set_ylim(0,10)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 72万知乎用户关注问题数，关注话题数，关注收藏夹数和关注专栏数分布\n",
    "# followingQuestionCount\n",
    "# followingTopicCount\n",
    "# followingFavlistsCount\n",
    "# followingColumnsCount\n",
    "fig, axes = plt.subplots(2,2)\n",
    "fig.set_size_inches(18,10)\n",
    "fig.suptitle('72万知乎用户关注问题数，关注话题数，关注收藏夹数和关注专栏数分布', fontsize=16, color=red)\n",
    "fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.2, hspace=0.5)\n",
    "\n",
    "# 把一个二维数组压扁\n",
    "subplotlist = list()\n",
    "for i in axes:\n",
    "    for j in i:\n",
    "        subplotlist.append(j)\n",
    "\n",
    "# 每个直方图的统计范围\n",
    "edge = [[0,100],\n",
    "       [100,1000],\n",
    "       [1000,10000],\n",
    "       [10000,40000],\n",
    "       ]\n",
    "# 每个直方图的组距\n",
    "widthlist = [1, 5, 50, 500]\n",
    "\n",
    "for i in range(len(subplotlist)):\n",
    "    tempList = [x for x in followingQuestionCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followingQuestionCountArray = np.array(tempList)\n",
    "    tempList = [x for x in followingTopicCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followingTopicCountArray = np.array(tempList)\n",
    "    tempList = [x for x in followingFavlistsCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followingFavlistsCountArray = np.array(tempList)\n",
    "    tempList = [x for x in followingColumnsCountList if x>=edge[i][0] and x<edge[i][1]]\n",
    "    followingColumnsCountArray = np.array(tempList)\n",
    "    \n",
    "    subplotlist[i].hist([followingQuestionCountArray,followingTopicCountArray,followingFavlistsCountArray,followingColumnsCountArray], \n",
    "                        normed=0, histtype='barstacked', label=['关注问题数','关注话题数','关注收藏夹数','关注专栏数'],  \n",
    "                        bins=int((edge[i][1]-edge[i][0])/widthlist[i]), color=[red,orange,green,brown], alpha = 0.7)\n",
    "    subplotlist[i].legend(loc='best')\n",
    "    subplotlist[i].set_xlim(edge[i][0], edge[i][1]) \n",
    "    subplotlist[i].set_title('%d-%d关注问题数，关注话题数，关注收藏夹数和关注专栏数（组距：%d）'%(edge[i][0],edge[i][1],widthlist[i]), \n",
    "                             color=red)\n",
    "    subplotlist[i].set_xlabel('关注问题数，关注话题数，关注收藏夹数和关注专栏数',color=red)\n",
    "    subplotlist[i].set_ylabel('用户数量（人）',color=red)\n",
    "    subplotlist[i].set_facecolor(orange2)\n",
    "    subplotlist[i].grid(True, linestyle='--')\n",
    "\n",
    "# 微调\n",
    "# subplotlist[3].set_ylim(0,10)\n",
    "\n",
    "fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
